function isnull(){
	return "<span class='red'>*</span>";
}
function formElement(fields){	
	 var element="";
	 if(fields.type=="text"){
		 element+="<input type='text' uikey='"+fields.caption+"' isnull='"+fields.notnull+"' id='"+fields.id+"'/>";
	 }
	 else if(fields.type=="select"){
	    element+="<select uikey='"+fields.caption+"' isnull='"+fields.notnull+"' id='"+fields.id+"'>";
	    element+="<option value=''></option>";
		for(var m=0;m<fields.source.options.length;m++){
			element+="<option  value="+fields.source.options[m].v+">"+fields.source.options[m].n+"</option>";
		}
		element+="</select>";
	 }
	 else if(fields.type=="webbox"){
		 element+="<textarea cols='4' uikey='"+fields.caption+"' isnull='"+fields.notnull+"' id='"+fields.id+"' rows='4'></textarea>";
		 //element+="<div class='upload'><span  onclick='loadImage();'>拍照</span><span onclick='loadImageLocal()'>获取本地</span><span></span></div>";
	 }
	 else if(fields.type=="source"){
		 element+="<input type='text' uikey='"+fields.caption+"' isnull='"+fields.notnull+"'  onclick='getSelectValue(this)' readOnly='true' id='"+fields.id+"'/>";
	 } 
	 else if(fields.type=="treebox"){
		 element+="<input type='text' uikey='"+fields.caption+"' isnull='"+fields.notnull+"'  onclick='getSelectValue(this)' readOnly='true' id='"+fields.id+"'/>";
	 } 
	 return element;
 }

function getSelectValue(obj){
        
}

function itemFormField(groups){
	var element="";
	for(var j=0;j<groups.fields.length;j++){
		if(groups.fields[j].caption!=undefined){
			element+="<tr><td class='td-l'>"+groups.fields[j].caption+":</td>";
			element+="<td id='"+groups.fields[j].id+"' type='"+groups.fields[j].type+"'>";
			element+=formElement(groups.fields[j]);
			element+=groups.fields[j].notnull ? isnull() :"";
			element+="</td></tr>";	
		}else{
			element+="<tr><td id='"+groups.fields[j].id+"' type='"+groups.fields[j].type+"' colspan='2'>";
			element+=formElement(groups.fields[j]);
			element+=groups.fields[j].notnull ? isnull() :"";
			element+="</td></tr>";	
		}				
	}
	return element;
 }
 function unitCondition(id,val){  
	 var item = new Condition(id,val);
	 return item;
 }
 //get submit condition 
function getCondion(type){
	var condition=[] ;
	$.each($("[name="+type+"] tr td[type]"),function(i,u){	
		var uitype=$(u).attr("type");
		if(uitype=="text"){
			 condition.push(unitCondition($(u).attr("id"),$(u).find("input").val()));	
		}else if(uitype=="select"){
			 condition.push(unitCondition($(u).attr("id"),$(u).find("select option:selected").val()));	
		}else if(uitype=="webbox"){
			 condition.push(unitCondition($(u).attr("id"),$(u).find("textarea").val()));		
		}else if(uitype=="number"){
			
		}
	});		
	return condition;	
}

function showMsgbox(name,message){
     $("[name='"+name+"']").html(message).show().delay(2000).fadeOut();
}

function submitUpate(){
	var result = true;
	$.each($("#addpage [isnull='true']"),function(i,u){
		if($(u).val()==""){
		  var text=($(u).attr("uikey")=='undefined') ?　"" : $(u).attr("uikey");
		  showMsgbox("prompt",text+"内容不能为空。");
    	  result = false;
    	  return false;   
		}
	})
	if(result)
	{
		zb.submit(zb.global.data.body.bill.tools[0].url,'bill' , 'init', '' , '' , zb.global.menu.header.session, 'update','add');
		showMsgbox("prompt","数据添加成功")
	}
}

 
function billAdd(){
	var bill=zb.global.data.body.bill;
	var groups=bill.groups;
	var html="<table name='update' class='grid'>";
	for(var i=0;i<groups.length;i++){
		if(groups[i].caption!=undefined){
			html+="<tr><td colspan='2' class='thead'>"+groups[i].caption+"</td></tr>";
			html+=itemFormField(groups[i]);
		}else{
			html+=itemFormField(groups[i]);
		}
	}
	html+="</table>"; 
	alert(html);
	$("#bill").empty().append(html);
}



/*拍照上传*/
 function loadImage() {
     //拍照并显示在屏幕
     navigator.camera.getPicture(onLoadImageSuccess, onLoadImageFail, {destinationType: Camera.DestinationType.DATA_URL});
 }
 //拍照成功后回调
 function onLoadImageSuccess(imageURI) {
     //这里的图片经过了base64编码
     var src = "data:image/jpeg;base64," + imageURI;
     $("#getImage").attr("src", src);
     $("#getImage").show();
 }
 //所有获取图片失败都回调此函数
 function onLoadImageFail(message) {
     navigator.notification.alert("拍照失败，原因：" + message, null, "警告");
 }
 function loadImageLocal() {
     //获取本地图片并显示在屏幕
     navigator.camera.getPicture(onLoadImageLocalSuccess, onLoadImageFail, {
         destinationType: Camera.DestinationType.FILE_URI,
         sourceType: Camera.PictureSourceType.PHOTOLIBRARY
     });
 }
 function onLoadImageLocalSuccess(imageURI) {
     $("#getImage").attr("src", imageURI);
     $("#getImage").show();
 }
 function loadImageUpload() {
     navigator.camera.getPicture(onLoadImageUploadSuccess, onLoadImageFail, {
         destinationType: Camera.DestinationType.FILE_URI
     });
 }
 //图片拍照成功后回调此函数
 function onLoadImageUploadSuccess(imageURI) {
     //此处执行文件上传的操作，上传成功后执行下面代码
     var options = new FileUploadOptions(); //文件参数选项
     options.fileKey = "file";//向服务端传递的file参数的parameter name
     options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);//文件名
     options.mimeType = "image/jpeg";//文件格式，默认为image/jpeg
     var ft = new FileTransfer();//文件上传类
     ft.onprogress = function (progressEvt) {//显示上传进度条
         if (progressEvt.lengthComputable) {
             navigator.notification.progressValue(Math.round(( progressEvt.loaded / progressEvt.total ) * 100));
         }
     }
     navigator.notification.progressStart("提醒", "当前上传进度");url : ""
     ft.upload(imageURI, encodeURI('http://192.168.1.127:908/mobilephone/upload.asp?userid='), function () {
         navigator.notification.progressStop();//停止进度条
         $("#getImageUpload").attr("src", imageURI);
         $("#getImageUpload").show();
         navigator.notification.alert("文件上传成功！", null, "提醒");
     }, null, options);
 }
 
 
 
 
 
 